#ifndef RSACRYPTO_H
#define RSACRYPTO_H
#include <QString>
#include <openssl/rsa.h>
#include <openssl/pem.h>

static const QString DirName = "RsaKey";
static const QString PubFileName = DirName + "/public.pem";
static const QString PriFileName = DirName + "/private.pem";

enum SignLevel
{
    Level1 = NID_md5,
    Level2 = NID_sha1,
    Level3 = NID_sha224,
    Level4 = NID_sha256,
    Level5 = NID_sha384,
    Level6 = NID_sha512
};

class RsaCrypto
{
public:
    enum KeyType{Public, Private};
    RsaCrypto();
    ~RsaCrypto();

    // 生成RSA密钥对
    void generateRsakey(int bits, QString pub = PubFileName, QString pri = PriFileName);
    // 将公钥/私钥字符串解析为RSA对象
    int parseStringToKey(QString keyStr, KeyType type = KeyType::Public);
    // 公钥加密
    QString rsaPubKeyEncrypt(QString data);
    // 私钥解密
    QString rsaPriKeyDecrypt(QString encData);
    // 使用RSA签名
    QString rsaSign(QString data, SignLevel level = Level3);
    // 使用RSA验证签名
    bool rsaVerify(QString data, QString signData, SignLevel level = Level3);
    // 得到pem格式公钥字符串
    QString publicPemKey(QString path = PubFileName);
    // 得到pem格式私钥字符串
    QString privatePemKey(QString path = PriFileName);

private:
    // 读文件中的pem格式的秘钥字符串
    QString getPemKey(QString path);

private:
    RSA* m_publicKey;	// 私钥
    RSA* m_privateKey;	// 公钥
};

#endif // RSACRYPTO_H
